<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>评论列表</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css">
  <style>
    .badge {
      float: right;
      margin-right: 5px;
    }

    .my-page {
      margin: 0 5px;
    }

    .all-page-content {
      margin-left: 5px;
    }

    .mt15 {
      margin-top: 15px;
    }
  </style>
</head>

<body style="padding: 15px;">

  <!-- 评论面板 -->
  <div class="panel panel-primary">
    <div class="panel-heading">
      <h3 class="panel-title">发表评论</h3>
    </div>
    <form class="panel-body cmt-form">
      <div>评论人：</div>
      <input type="text" class="form-control" name="username" autocomplete="off" />
      <div>评论内容：</div>
      <textarea class="form-control" name="content"></textarea>
      <button type="submit" class="btn btn-primary submit">发表评论</button>
    </form>
  </div>


  <!-- 评论列表 -->
  <ul class="mt15 list-group">
    <!-- <li class="list-group-item">
       <span>评论内容</span>
       <span class="badge del" style="cursor:pointer; background-color: lightgray;">删除</span> 
       <span class="badge" style="background-color: #F0AD4E;">评论时间：xxx</span>
       <span class="badge" style="background-color: #5BC0DE;">评论人：xxx</span>
    </li> -->
  </ul>

  <!-- 分页器 -->
  <nav class="mt15">
    <ul class="pagination">
      <li>
        <button class="last">
          <span>&laquo;</span>
        </button>
      </li>
      <li class="my-page">
        <span class="page-show"></span>
      </li>
      <li>
        <button class="next">
          <span>&raquo;</span>
        </button>
      </li>
      <li class="all-page-content">
        <span>共计:<span class="all-page"></span>页</span>
      </li>
    </ul>
  </nav>
  <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
  <script src="./lib/form-serialize.js"></script>
  <script>
    let allPage
    let nowPage = 1
    const geturl = 'https://hmajax.itheima.net/api/cmtlist'
    const posturl = 'https://hmajax.itheima.net/api/addcmt'
    const delurl = 'https://hmajax.itheima.net/api/delcmt'
    // console.log(geturl)

    //获取单页评论数据并渲染
    async function getList(url, page) {
      const res = await axios.get(url + `/?page=${page}`)
      allPage = res.data.allPage
      console.log(allPage)
      // console.log(res.data)
      render(res.data)
    }

    getList(geturl, nowPage)
    //渲染
    function render(data) {
      let datalist = data.data
      let str = datalist.map(item => `<li class="list-group-item">
       <span>${item.content}</span>
       <span class="badge del" style="cursor:pointer; background-color: lightgray;" data-id=${item.id}>删除</span> 
       <span class="badge" style="background-color: #F0AD4E;">评论时间：${item.time}</span>
       <span class="badge" style="background-color: #5BC0DE;">评论人：${item.username}</span>
    </li>`
      ).join('')
      document.querySelector('.list-group').innerHTML = str
      document.querySelector('.all-page').innerHTML = allPage
      document.querySelector('.page-show').innerHTML = nowPage
    }

    //注册换页码事件
    document.querySelector('.next').onclick = function () {
      nowPage++
      if (nowPage > allPage) {
        nowPage = allPage
        return
      }
      getList(geturl, nowPage)
    }
    //上一页
    document.querySelector('.last').onclick = function () {
      nowPage--
      if (nowPage < 1) {
        nowPage = 1
        return
      }
      getList(geturl, nowPage)
    }

    //新增评论，把新增的评论添加到列表最前面
    function addRemark(data) {
      const form = document.querySelector('.cmt-form')
      const remarkData = serialize(form, { hash: true, empty: true })
      console.log( '表单数据' , remarkData)
      axios.post(posturl, remarkData)
        .then(res => {
          //添加成功后跳转到第一页渲染
          nowPage = 1
          getList(geturl, nowPage)
        })
    }
    document.querySelector('.submit').onclick = function (e) {
      e.preventDefault()
      addRemark()
    }

    //删除评论
    document.querySelector('.list-group').onclick = function (e) {
      if (e.target.className === 'badge del') {
        const id = e.target.dataset.id
        axios.get(delurl + `/?id=${id}`)
          .then(res => {
            //删除成功后刷新当前页
            console.log(res.data.allPage)
            // 判断是否是最后一页，如果是，则返回上一页
            // 同时保证当前前面不会小于1 
            if (res.data.allPage !== allPage) {
              nowPage--
              nowPage = nowPage < 1 ? 1 : nowPage
            }
            getList(geturl, nowPage)
          })
      }
    }


  </script>
</body>

</html>